package com.ms.mcv.login.controller;

import com.ms.mcv.common.dto.UserLoginDto;
import com.ms.mcv.common.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;

@Controller
@Slf4j
@RequestMapping("/api/auth")
public class AuthController {
    @PostMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, HttpServletResponse response,@RequestBody UserLoginDto userLoginDto) {
        JSONObject resJson = new JSONObject();
        if (Optional.ofNullable(userLoginDto.getUsername()).isPresent() &&
                ("ant.design".equals(userLoginDto.getPassword()) ||
                        "admin".equals(userLoginDto.getUsername()))
        ) {
            UserVo userVo = getAdminUserVo();
            resJson.put("result",JSONObject.fromObject(userVo).toString());
            resJson.put("message","认证成功");
            resJson.put("code",200);
            resJson.put("_status", 200);
            resJson.put("token",userVo.getToken());
            request.getSession().setAttribute("currentUser", userVo);
            return resJson.toString();
        } else if(Optional.ofNullable(userLoginDto.getMobile()).isPresent()){
            UserVo userVo =  getAdminUserVo();
            resJson.put("result",JSONObject.fromObject(userVo).toString());
            resJson.put("message","认证成功");
            resJson.put("code",200);
            resJson.put("_status", 200);
            resJson.put("token",userVo.getToken());
            request.getSession().setAttribute("currentUser", userVo);
            return resJson.toString();
        }
        JSONObject result = new JSONObject();
        result.put("isLogin", true);
        resJson.put("result",result);
        resJson.put("message","错误的用户名和密码，请确认后重试！");
        resJson.put("code",401);
        resJson.put("_status", 401);
        return resJson.toString();
    }

    @PostMapping("/2step-code")
    @ResponseBody
    public JSONObject twoFactor() {
        JSONObject resJson = new JSONObject();
        JSONObject dataJson = new JSONObject();
        dataJson.put("stepCode",new Random().nextInt(1));
        resJson.put("result", dataJson);
        return resJson;
    }

    @PostMapping("/logout")
    @ResponseBody
    public JSONObject logout(HttpServletRequest request) {
        request.getSession().removeAttribute("currentUser");
        JSONObject data = new JSONObject();
        data.put("result", new JSONObject());
        data.put("message", "");
        return data;
    }

    private UserVo getAdminUserVo() {
        UserVo userVo = new UserVo();
        userVo.setId(UUID.randomUUID().toString());
        userVo.setName("超级管理员");
        userVo.setUsername("admin");
        userVo.setAvatar("https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png");
        userVo.setStatus(1);
        userVo.setLastLoginTime(System.currentTimeMillis());
        userVo.setCreatorId("admin");
        userVo.setCreateTime(System.currentTimeMillis());
        userVo.setRoleId("admin");
        userVo.setLang("zh-CN");
        userVo.setToken("4291d7da9005377ec9aec4a71ea837f");
        return userVo;
    }
}
